

package cn.shengchao.examstar.pay.ohs.remote.rpc;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.shengchao.examstar.pay.infrastructure.dao.po.PayOrder;
import cn.shengchao.examstar.pay.infrastructure.dao.po.PayOrderItem;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.orders.api.IOrderRpc;
import cn.shengchao.examstar.rpc.orders.req.OrderQueryPage;
import cn.shengchao.examstar.rpc.orders.req.Refund;
import cn.shengchao.examstar.rpc.orders.req.RefundQueryPage;
import cn.shengchao.examstar.rpc.orders.req.RemarkReq;
import cn.shengchao.examstar.pay.acl.ports.repositories.IOrderRepository;
import cn.shengchao.examstar.pay.model.entity.OrderEntity;
import cn.shengchao.examstar.pay.model.valobj.OrderStatusVO;
import cn.shengchao.examstar.rpc.orders.res.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import top.continew.starter.core.validation.CheckUtils;

import java.math.BigDecimal;
import java.util.List;

@DubboService
public class OrderRpc implements IOrderRpc {

    @Autowired
    private IOrderRepository iOrderRepository;

    @Override
    public PageResult<OrderList> getOrderList(OrderQueryPage orderQueryPage) throws Exception {
        Page<Object> page = PageHelper.startPage(orderQueryPage.getPage(), orderQueryPage.getSize());
        List<OrderList> orderLists = iOrderRepository.getOrderList(orderQueryPage);
        for (OrderList orderList : orderLists) {
            orderList.setPhone(DesensitizedUtil.mobilePhone(orderList.getPhone()));
        }
        return PageResult.of(page.getTotal(), orderLists);
    }

    @Override
    public OrderInfo getOrderInfo(String orderId) {
        OrderInfo orderInfo = new OrderInfo();
        List<OrderItemInfo> orderItemInfos = iOrderRepository.queryOrderItemList(orderId);
        orderInfo.setOrderItemInfos(orderItemInfos);
        List<OrderChangeLog> changeLogs = iOrderRepository.queryOrderChangeLogs(orderId);
        orderInfo.setOrderChangeLogs(changeLogs);
        List<Remarks> remarks = iOrderRepository.getOrderRemarks(orderId);
        orderInfo.setRemarks(remarks);
        return orderInfo;
    }

    @Override
    public PayOrderInfoDto getOrderInfoDto(Long orderId) {
        PayOrderInfoDto orderInfo = new PayOrderInfoDto();
        PayOrder payOrder=iOrderRepository.queryOrderInfoById(orderId);
        if(payOrder!=null){
            BeanUtil.copyProperties(payOrder,orderInfo);
            List<PayOrderItem> orderItemInfos = iOrderRepository.queryOrderItemInfosById(orderId);
            if (CollUtil.isNotEmpty(orderItemInfos)) {
                orderInfo.setOrderItemInfos(BeanUtil.copyToList(orderItemInfos, PayOrderItemInfoDto.class));
            }
        }
        return orderInfo;
    }

    @Override
    public void refunds(Refund refund) throws Exception {
        OrderEntity orderEntity = iOrderRepository.queryOrder(refund.getOrderId());
        CheckUtils.throwIfNull(orderEntity, "订单不存在");
        CheckUtils.throwIf(orderEntity.getOrderStatus() != OrderStatusVO.PAY_SUCCESS && orderEntity
            .getOrderStatus() != OrderStatusVO.PARTIAL_REFUND, "订单不支持退款");
        if (refund.getOrderItemId() == null) {
            //已退费金额
            BigDecimal refundSum = iOrderRepository.getOrderRefundsSum(refund.getOrderId());
            //本次退费金额
            BigDecimal refundPrice = orderEntity.getTotalAmount().subtract(refundSum);
            if (refundPrice.compareTo(BigDecimal.ZERO) > 0) {
                //退费
                iOrderRepository.refunds(refund.getUserId(), refund.getOrderId(), refund
                    .getOrderItemId(), refundPrice, refund.getRefundReason(), refund.getRemark(), true);
            }
        } else {
            iOrderRepository.refunds(refund.getUserId(), refund.getOrderId(), refund.getOrderItemId(), null, refund
                .getRefundReason(), refund.getRemark(), false);
        }
    }

    @Override
    public void remarks(RemarkReq remarkReq) {
        iOrderRepository.saveOrderRemarks(remarkReq.getOrderId(), remarkReq.getRemarks(), remarkReq.getUserId());
    }

    @Override
    public PageResult<RefundList> getOrderRefund(RefundQueryPage refundQueryPage) {
        Page<RefundList> page = PageHelper.startPage(refundQueryPage.getPage(), refundQueryPage.getSize());
        List<RefundList> refundLists = iOrderRepository.getOrderRefund(refundQueryPage);
        return PageResult.of(page.getTotal(), refundLists);
    }
}
